

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var id = user.id;
    compare();
    function compare() {
        var goods_list = getLocalData();
        var goods_ids = [];
        var list = [];
        var attr = [];
        $('.purchase').find("td").remove();
        if (goods_list != undefined) {
            for (var i = 0; i < goods_list.length; i++) {
                var obj = goods_list[i];
                goods_ids.push(obj.productID);
                obj.productID;
            }
            var params = {
                "goods_ids": goods_ids

            };
            send_ajax('post', '/api/goods/compare', params, function callback(result) {
                if (result.code == 200&&result.data) {
                    var html = "";
                    list = result.data;
                    reloadData();
                    attrGroup();
                    for (var i = 0; i < list.length; i++) {
                        html += '\n                            <td>\n                                <a class="btn_add" data-goodsId=' + list[i].id + '>\n                                \u52A0\u5165\u8D2D\u7269\u8F66\n                                </a>&nbsp;&nbsp;\n                                <a class="add_store" data-goodsId=' + list[i].id + '>\n                                    \u52A0\u5165\u6536\u85CF\u5939 \n                                </a>\n                            </td>';
                    }
                    $('.purchase').append(html);
                }
            });
        };
        function reloadData() {
            $('#imgs').find('td').remove();
            $('#title').find('td').remove();
            $('#cat').find('td').remove();
            $('#artist').find('td').remove();
            $('#price').find('td').remove();
            $('#name').find('td').remove();
            $('#sycs').find('td').remove();
            // 《${list[i].Spu.spu_name?list[i].Spu.spu_name:'----'}》
            for (var _i = 0; _i < list.length; _i++) {
                $('#imgs').append('<td class="compare_img_td"><a href="/goods-'+ list[_i].id +'.html" target="_blank"> \n                <div class="hacyimgload"\n "><img src=" ' + (list[_i].mainImg ? isImgHttp(list[_i].mainImg) : '----') + '" alt="' + (list[_i].name ? list[_i].name : '----') + '"></div></a><i data-id=' + list[_i].id + ' class="icon_error"></i></td>');
                $('#title').append('<td><a href="/goods-'+ list[_i].id +'.html" target="_blank">' + (list[_i].name ? list[_i].name : '----') + '<strong>\n                <font color="' + (list[_i].goodsAdNameStyle ? list[_i].goodsAdNameStyle : '----') + '">' + (list[_i].goodsAdName ? list[_i].goodsAdName : '----') + '</font>\n            </strong></a></td>');
                $('#cat').append('<td><a >' + (list[_i].catName ? list[_i].catName : '----') + '</a>\n            </td>');
                $('#artist').append('<td><a >' + (list[_i].artistName ? list[_i].artistName : '----') + '</a></td>');
                $('#price').append('<td><b>\uFFE5</b>' + (list[_i].goodsPrice ? list[_i].goodsPrice : '----') + '</td>');
                $('#name').append('<td><a>' + (list[_i].name ? list[_i].name : '---') + '</a></td>');
                $('#sycs').append('<td><a>----</a></td>');
                $('#use').append('<td><a>----</a></td>');
                $('#specification').append('<td><a>----</a></td>');
                $('#property').append('<td><a>----</a></td>');
                $('#texture').append('<td><a>----</a></td>');
                $('#size').append('<td><a">----</a></td>');
                $('#status').append('<td><a>----</a></td>');
            }
        }
        //商品的分类左侧、如山水分类
        function attrGroup() {
            var html = "";
            if (list.length > 0) {
                if (list[0].attrList) {
                    for (var i = 0; i < list[0].attrList.length; i++) {
                        html += ' <tr class="attr">\n                            <th>' + list[0].attrList[i].group + '</th>\n                        </tr>';
                    };
                }
            }

            $('.compar_page_list .attr').remove();
            $('.purchase').before(html);
            var el = $('.attr');
            for (var k = 0; k < list.length; k++) {
                for (var j = 0; j < list[k].attrList.length; j++) {
                    var item = list[k].attrList[j];
                    $(el[j]).append('<td><a target="_blank">' + (item.attrs ? item.attrs : "---") + '</a></td>');
                }
            }
        };
    }
    function getLocalData() {
        var local_goods_array = $.cookie('goods_list');
        if (local_goods_array != undefined) {
            var localData = JSON.parse(local_goods_array);
            return localData;
        };
        return null;
    };
    $(document).on('click', '.icon_error', function () {
        var goods_id = $(this).attr("data-id");
        var locaData = getLocalData();
        locaData = locaData.filter(function (item) {
            return item.productID != goods_id;
        });
        $.cookie('goods_list', JSON.stringify(locaData), { path: '/' });
        compare();
    });
    $(document).on('click', '.add_store', function () {
        if (!id) {
            alert('您还没有登录，请登录后再收藏！');return;
        }
        var goods_id = $(this).attr("data-goodsid");
        var isStore_param = {
            "GoodsCollect": {
                "user_id": id,
                "goods_id": goods_id
            }
        };
        send_ajax('post', '/head', isStore_param, function callback(data) {
            if (data.GoodsCollect.count > 0) {
                alert('已经收藏过了！');
                return;
            } else {
                var param = {
                    "GoodsCollect": {
                        "goods_id": goods_id,
                        "user_id": id
                    },
                    "tag": "GoodsCollect"
                };
                send_ajax('post', '/post', param, function callback(data) {
                    if (data.code == 200) {
                        alert('收藏成功');
                    }
                });
            }
        });
    });
});